import 'dart:async';
import 'dart:convert';

import 'package:get/get.dart';
import 'package:xin_he/page/shopping_car/xin_he_goods_payment/xin_he_pay_success/xin_he_order_detail_entity.dart';

import '../../../../service/xin_he_reqest.dart';
import '../../../../utils/xin_he_storage.dart';
import '../../../../utils/xin_he_toast.dart';
import 'xin_he_pay_success_state.dart';

class XinHePaySuccessLogic extends GetxController {
  final XinHePaySuccessState state = XinHePaySuccessState();

  RxInt loading = 0.obs;///0未支付 1支付成功
  Timer? _timer;
  Rx<XinHeOrderDetailEntity> orderModel = XinHeOrderDetailEntity().obs;
  Map orderMap = {};

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    orderMap = Get.arguments;
    _timer = Timer.periodic(const Duration(seconds: 3), (Timer timer) async {

      await queryOrderStatus();
    });

    }

  @override
  void onReady() async{
    // TODO: implement onReady
    super.onReady();


  }


  queryOrderStatus() async {


    var userId = await XinHeLocalStorage.get(XinHeStorage.userId);
    Map<String,dynamic> param = {
    'orderId': orderMap['orderId']
    };
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/order/detailOrder',params: param,isPost: false);

    if(!result.result){
    XinHeToast.center(result.data['message']);
    return ;
    }

    if(result.data['code'] == 0 ){

      XinHeOrderDetailEntity model = XinHeOrderDetailEntity.fromJson(result.data['data']);
      orderModel.value = model;
      if(model.status == 'UN_PAY') {
        loading.value == 0;

      }else if (model.status == 'PAYED') {
        loading.value == 1;

        if(_timer != null) {
          _timer?.cancel();
          _timer = null;
        }
      }

    }

  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    if(_timer != null) {
      _timer?.cancel();
      _timer = null;
    }
  }
}
